﻿using Core.Tools.Helper;
using Core.Tools.IOC;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Core.Tools.Config
{
    [ExposedService(Lifttime.Singleton, typeof(IConfigManager))]
    class ConfigManager : IConfigManager
    {
        private const string root = "Config";

       
        public T Read<T>(ValueType key)
        {
            string fileName = GetFullPath(key);

            return JsonHelper.Load<T>(fileName);
        }

        private string GetFullPath(ValueType key)
        {
            return  Path.Combine(root, GetFullName(key));
        }

        public void Write(ValueType key, object value)
        {
            Directory.CreateDirectory(root);
            string fileName = GetFullPath(key);
            JsonHelper.Write(value, fileName, true);
        }

        private string GetFullName(ValueType key)
        {
            return $"{key.GetType().FullName}.{key}.json";
        }
    }
}
